package com.yunlankeji.stemcells.activity.index;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.hwangjr.rxbus.RxBus;
import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.ganxibaozhijia.databinding.ActivityMainBinding;
import com.yunlankeji.stemcells.BaseApplication;
import com.yunlankeji.stemcells.utils.BusAction;
import com.zackratos.ultimatebarx.library.UltimateBarX;
import com.zackratos.ultimatebarx.library.bean.BarConfig;

import static com.yunlankeji.stemcells.activity.release.ReleaseActivity.startReleaseActivity;

/**
 * @author yang
 */
public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        UltimateBarX.with(this)
                .config(new BarConfig().fitWindow(false).color(Color.TRANSPARENT))
                .applyStatusBar();
        View view = findViewById(R.id.li_main);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.setBitmap(getBitmap());
                startReleaseActivity(MainActivity.this);
            }
        });
        binding.mHomeLl.setOnClickListener(v -> {
            showView(0);
            RxBus.get().post(BusAction.Refresh_Home, "Refresh_Home");
        });
    }

    /**
     * 页面切换
     * @param i 第i+1个页面
     */
    @SuppressLint("ResourceType")
    public void showView(int i) {

        int colorT = Color.parseColor("#49B1FD");
        int colorF = Color.parseColor("#CDCDCD");

        binding.mHomeTv.setTextColor(this.getResources().getColor(i == 0 ? colorT : colorF));
        binding.mHomeIv.setImageResource(i == 0 ? R.mipmap.home_1_t : R.mipmap.home_1_f);

        binding.mVideoTv.setTextColor(this.getResources().getColor(i == 1 ? colorT : colorF));
        binding.mVideoIv.setImageResource(i == 1 ? R.mipmap.home_sp_2_t : R.mipmap.home_sp_2_f);

        binding.mProjectTv.setTextColor(this.getResources().getColor(i == 3 ? colorT : colorF));
        binding.mProjectIv.setImageResource(i == 3 ? R.mipmap.home_xm_4_t : R.mipmap.home_xm_4_f);

        binding.mMineTv.setTextColor(this.getResources().getColor(i == 4 ? colorT : colorF));
        binding.mMineIv.setImageResource(i == 4 ? R.mipmap.home_wd_5_t : R.mipmap.home_wd_5_f);

        binding.viewPager.setCurrentItem(i, false);

    }
    /**
     * 获取图片用于高斯模糊
     */
    private Bitmap getBitmap(){
        View view = getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        return bitmap;
    }

    private long time = 0;
    @Override
    public void onBackPressed() {
        long temp = System.currentTimeMillis();
        if (time == 0 || temp - time > 1000 * 2) {
            time = temp;
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_LONG).show();
        } else {
            finish();
        }
    }

}